<?php
/**
 * 用户登录注册模块
 * 
 + ----------------------------------------------------- 
 * Date 2013-3-8
 + -----------------------------------------------------
 */
class PassportAction extends BaseAction {
	
	public function index(){
		
		$this->display();
	}
	
	public function login(){
		$this->assign('sub_menu',$this->_getNavigation());
		$this->assign('current_menu',array('menu_name'=>'User Login'));
		$this->display();
	}
	
	public function ajaxLogin(){
		$username = $_POST['username'];
		$password = $_POST['password'];
		$map=array(
			'username'	=>	$username,
			'password'	=>	md5($password),
		);
		$data=array(
			'info'	=>	'',
			'status'=>	0
		);
		if(empty($username)){
			$this->error('请输入用户名！');
		}elseif(empty($password)){
			$this->error('请输入密码！');
		}
		$model = M("User");
		$user = $model->field('password',true)->where($map)->find();
		if($user['id']){
			$_SESSION['UserInfo']['user_id'] = $user['id'];
			$_SESSION['UserInfo']['username'] = $user['username'];
			$_SESSION['UserInfo']['email'] = $user['email'];
			$_SESSION['UserInfo']['mobile'] = $user['mobile'];
			$data['status']	=	1;
			$data['info']='登录成功！';
		}else{
			$data['info']='登录用户名或密码错误！';
		}
		echo json_encode($data);
	}
	
	public function dologin(){
		$username	=	trim($_POST['username']);
		$password	=	trim($_POST['password']);
		$code	=	trim($_POST['code']);
		$map=array(
			'username'	=>	$username,
			'password'	=>	md5($password),
		);
		if(empty($username)){
			$this->error('Fill the email please！');
		}elseif(empty($password)){
			$this->error('Fill the password please！');
		}elseif($_SESSION['code'] != md5($code)){
			$this->error('Not correct code！');
		}
		$model = M("User");
		$user = $model->field('password',true)->where($map)->find();
		if($user['id']){
			$_SESSION['UserInfo']['user_id'] = $user['id'];
			$_SESSION['UserInfo']['username'] = $user['username'];
			$_SESSION['UserInfo']['email'] = $user['email'];
			$_SESSION['UserInfo']['mobile'] = $user['mobile'];
			$this->success('Login Success！',__APP__.'/');
		}else{
			$this->error('Login faild, Not correct username or password！');
		}
	}
	
	public function register(){
		$this->assign('sub_menu',$this->_getNavigation());
		$this->assign('current_menu',array('menu_name'=>'Registrition'));
		
		$this->display();
	}
	public function download(){
		$this->assign('sub_menu',$this->_getNavigation());
		$this->assign('current_menu',array('menu_name'=>'Download'));

		$this->display();
	}
	public function openAccount(){
		$this->assign('sub_menu',$this->_getNavigation());
		$this->assign('current_menu',array('menu_name'=>'Open an account'));

		$this->display();
	}
	
	public function doregister(){
		
		$username	=	trim($_POST['email']);
		$password	=	trim($_POST['password']);
		$confirm_password	=	trim($_POST['confirm_password']);
		$mobile	=	trim($_POST['mobile']);
		$code	=	trim($_POST['code']);
		$data=array(
			'username'	=>	$username,
			'password'	=>	md5($password),
			'mobile'	=>	$mobile,
			'add_time'	=>	time(),
		);
		if(empty($username)){
			$this->error('Fill the email please！');
		}elseif(!regex($username,'email')){
			$this->error('Corrent email！');
		}elseif(!$this->checkUser($username)){
			$this->error('The email has registed！');
		}elseif(empty($password)){
			$this->error('Fill the password please！');
		}elseif(empty($confirm_password)){
			$this->error('Confirm the password please！');
		}elseif($confirm_password != $password){
			$this->error('Confirm the password please！');
		}elseif(empty($mobile)){
			$this->error('Fill the mobile number please！');
		}elseif(!regex($mobile,'mobile')){
			$this->error('Not correct mobile number！');
		}elseif($_SESSION['code'] != md5($code)){
			$this->error('Not correct code！');
		}
		$model = M("User");
		$res_id = $model->add($data);
		if($res_id){
			$_SESSION['UserInfo']['user_id'] = $res_id;
			$_SESSION['UserInfo']['username'] = $data['username'];
			$_SESSION['UserInfo']['mobile'] = $data['mobile'];
			$this->success('Regist success, please login！',__URL__."/login");
		}else{
			$this->error('Sorry , regist faild！');
		}
		
	}
	
	public function logout(){
		$_SESSION['UserInfo']=array();
		unset($_SESSION['UserInfo']);
		session_destroy();
		$this->success('Login out success ！',__APP__."/");
	}
	
	// 检查用户名是否存在
	protected function checkUser($username){
		$model = M('User');
		if($model->where("username='".$username."'")->count() > 0){
			return false;
		}else{
			return true;
		}
	}
	
}
?>
